package ${package.Entity}

import com.dongql.mybatis.tenant.annotations.MultiTenant
import com.dongql.mybatis.tenant.annotations.MultiTenantType
import com.lgl.bean.base.BaseEntity
import com.lgl.enums.status.StatusEnum
import com.lgl.enums.status.YesNo
import java.util.*
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.Table

/**
 * <p>
 * ${table.comment}
 * </p>
 *
 * @author ${author}
 */
<#if table.convert>
@TableName("${table.name}")
</#if>
@Table
@MultiTenant(type = MultiTenantType.SCHEMA)
<#if swagger2>
@ApiModel(value="${entity}对象", description="${table.comment!}")
</#if>
<#if superEntityClass??>
open class ${entity} : ${superEntityClass}<#if activeRecord><${entity}></#if>() {
<#elseif activeRecord>
class ${entity} : Model<${entity}>() {
<#else>
class ${entity} : Serializable {
</#if>

<#-- ----------  BEGIN 字段循环遍历  ---------->
<#list table.fields as field>
<#if field.keyFlag>
    <#assign keyPropertyName="${field.propertyName}"/>
</#if>

<#if field.comment!?length gt 0>
<#if swagger2>
        @ApiModelProperty(value = "${field.comment}")
<#else>
    /**
     * ${field.comment}
     */
</#if>
</#if>
<#if field.keyFlag>
<#-- 主键 -->
    @Id
<#if field.keyIdentityFlag>
    @GeneratedValue(strategy = GenerationType.IDENTITY)
</#if>
<#-- 普通字段 -->
<#elseif field.fill??>
<#-- -----   存在字段填充设置   ----->
<#if field.convert>
    @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
<#else>
    @TableField(fill = FieldFill.${field.fill})
</#if>
<#elseif field.convert>
    @TableField("${field.annotationColumnName}")
</#if>
<#-- 乐观锁注解 -->
<#if (versionFieldName!"") == field.name>
    @Version
</#if>
<#-- 逻辑删除注解 -->
<#if (logicDeleteFieldName!"") == field.name>
    @TableLogic
</#if>
    <#if field.select>
    var ${field.propertyName}: StatusEnum? = null

    @Transient
    var ${field.propertyName}Desc: String? = null
        get() = ${field.propertyName}?.description
    <#elseif field.propertyName == "locationId">
    var ${field.propertyName}: Long? = null

    @Transient
    var locationName: String? = null
    <#elseif field.propertyType == "Integer">
    var ${field.propertyName}: Int? = null
    <#elseif field.propertyType == "LocalDateTime" || field.propertyType == "LocalDate">
    var ${field.propertyName}: Date? = null
    <#else>
    var ${field.propertyName}: ${field.propertyType}? = null
    </#if>
</#list>
<#-- ----------  END 字段循环遍历  ---------->

    fun diff(entity: ${entity}): String {
        val diff = StringJoiner("\n")
<#list table.fields as field>
    <#if field.propertyName == "idx" || field.propertyName == "createTime">
    <#else>
        if (${field.propertyName} != entity.${field.propertyName}) {
            diff.add("${field.comment}: ${r'$'}${field.propertyName} -> ${r'${entity.'}${field.propertyName}}")
        }
    </#if>
</#list>
        return diff.toString()
    }


<#if entityColumnConstant>
    companion object {
<#list table.fields as field>
        const val ${field.name?upper_case}: String = "${field.name}"
</#list>
    }


</#if>
<#if activeRecord>
    override fun pkVal(): Serializable? {
<#if keyPropertyName??>
        return ${keyPropertyName}
<#else>
        return null
</#if>
    }

</#if>
    override fun toString(): String {
        return "${entity}{" +
<#list table.fields as field>
<#if field_index==0>
                "${field.propertyName}=" + ${field.propertyName} +
<#else>
                ", ${field.propertyName}=" + ${field.propertyName} +
</#if>
</#list>
                "}"
    }
}
